/**
 * ChatMessagePushlet.java
 */
package com.tobeface.sns.infrastructure.pushlet;

import java.io.Serializable;

import nl.justobjects.pushlet.core.Event;
import nl.justobjects.pushlet.core.EventPullSource;

import com.tobeface.sns.domain.message.ChatMessage;
import com.tobeface.sns.infrastructure.amqp.ChatMessageCustomer;

/**
 * @author Dylan
 * @time 2012-8-3
 */
public class ChatMessagePushlet implements Serializable {

	private static final long serialVersionUID = 1L;
	

	public static class ChatMessageHandle extends EventPullSource {

		private ChatMessageCustomer customer = new ChatMessageCustomer();
		/*
		 * (non-Javadoc)
		 * 
		 * @see nl.justobjects.pushlet.core.EventPullSource#getSleepTime()
		 */
		@Override
		protected long getSleepTime() {
			return 5000;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see nl.justobjects.pushlet.core.EventPullSource#pullEvent()
		 */
		@Override
		protected Event pullEvent() {
			ChatMessage message = customer.getMessage();
			if (null == message) {
				return Event.createDataEvent("");
			}
			Event event = Event.createDataEvent("/" + message.getReceiverName());
			event.setField("msg", message.getMsg());
			System.out.println(message.getReceiverName() + " : " + message.getMsg());
			return event;
		}

	}

}
